<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="送货单号" prop="deliveryNumber">
        <el-input
          v-model="queryParams.deliveryNumber"
          placeholder="请输入送货单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="送货日期">
        <el-date-picker
          v-model="daterangeDeliveryDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="出货仓库" prop="warehouse">
        <el-select v-model="queryParams.warehouse" placeholder="请选择出货仓库" clearable>
          <el-option
            v-for="dict in dict.type.warehouse"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客户" prop="customeId">
        <el-input
          v-model="queryParams.customeId"
          placeholder="请输入客户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="contact">
        <el-input
          v-model="queryParams.contact"
          placeholder="请输入联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="送货地址" prop="deliveryAddress">
        <el-input
          v-model="queryParams.deliveryAddress"
          placeholder="请输入送货地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单据备注" prop="memo">
        <el-input
          v-model="queryParams.memo"
          placeholder="请输入单据备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="打印状态" prop="printStatus">
        <el-select v-model="queryParams.printStatus" placeholder="请选择打印状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="auditsStatus">
        <el-select v-model="queryParams.auditsStatus" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['selling:delivery:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['selling:delivery:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['selling:delivery:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['selling:delivery:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deliveryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="" align="center" prop="id" />
      <el-table-column label="送货单号" align="center" prop="deliveryNumber" />
      <el-table-column label="送货日期" align="center" prop="deliveryDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliveryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="客户" align="center" prop="customeId" />
      <el-table-column label="联系人" align="center" prop="contact" />
      <el-table-column label="单据备注" align="center" prop="memo" />
      <el-table-column label="打印状态" align="center" prop="printStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.printStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="auditsStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.auditsStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['selling:delivery:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['selling:delivery:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改销售送货单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="送货日期" prop="deliveryDate">
          <el-date-picker clearable
            v-model="form.deliveryDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择送货日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="出货仓库" prop="warehouse">
          <el-select v-model="form.warehouse" placeholder="请选择出货仓库">
            <el-option
              v-for="dict in dict.type.warehouse"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户" prop="customeId">
          <el-input v-model="form.customeId" placeholder="请输入客户" />
        </el-form-item>
        <el-form-item label="客户地址" prop="customeAddress">
          <el-input v-model="form.customeAddress" placeholder="请输入客户地址" />
        </el-form-item>
        <el-form-item label="联系人" prop="contact">
          <el-input v-model="form.contact" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactNumber">
          <el-input v-model="form.contactNumber" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="客户传真" prop="customFax">
          <el-input v-model="form.customFax" placeholder="请输入客户传真" />
        </el-form-item>
        <el-form-item label="单据备注" prop="memo">
          <el-input v-model="form.memo" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="送货地址" prop="deliveryAddress">
          <el-input v-model="form.deliveryAddress" placeholder="请输入送货地址" />
        </el-form-item>
        <el-form-item label="合同号码" prop="contractNumber">
          <el-input v-model="form.contractNumber" placeholder="请输入合同号码" />
        </el-form-item>
        <el-form-item label="发票号码" prop="invoiceNumber">
          <el-input v-model="form.invoiceNumber" placeholder="请输入发票号码" />
        </el-form-item>
        <el-form-item label="付款方式" prop="payment">
          <el-select v-model="form.payment" placeholder="请选择付款方式">
            <el-option
              v-for="dict in dict.type.payment"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="送货司机" prop="diver">
          <el-input v-model="form.diver" placeholder="请输入送货司机" />
        </el-form-item>
        <el-form-item label="快递信息" prop="courier">
          <el-input v-model="form.courier" placeholder="请输入快递信息" />
        </el-form-item>
        <el-form-item label="包装员" prop="packer">
          <el-input v-model="form.packer" placeholder="请输入包装员" />
        </el-form-item>
        <el-form-item label="物流单号" prop="logistic">
          <el-input v-model="form.logistic" placeholder="请输入物流单号" />
        </el-form-item>
        <el-form-item label="运费" prop="freight">
          <el-input v-model="form.freight" placeholder="请输入运费" />
        </el-form-item>
        <el-form-item label="打印状态" prop="printStatus">
          <el-select v-model="form.printStatus" placeholder="请选择打印状态">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核状态" prop="auditsStatus">
          <el-select v-model="form.auditsStatus" placeholder="请选择审核状态">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-divider content-position="center">销售送货单子信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddRedDeliverySub">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteRedDeliverySub">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="redDeliverySubList" :row-class-name="rowRedDeliverySubIndex" @selection-change="handleRedDeliverySubSelectionChange" ref="redDeliverySub">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="产品id" prop="goodsId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.goodsId" placeholder="请输入产品id" />
            </template>
          </el-table-column>
          <el-table-column label="数量" prop="num" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.num" placeholder="请输入数量" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDelivery, getDelivery, delDelivery, addDelivery, updateDelivery } from "@/api/selling/delivery";

export default {
  name: "Delivery",
  dicts: ['warehouse', 'sys_yes_no', 'payment'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedRedDeliverySub: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售送货单表格数据
      deliveryList: [],
      // 销售送货单子表格数据
      redDeliverySubList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 数量时间范围
      daterangeDeliveryDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deliveryNumber: null,
        deliveryDate: null,
        warehouse: null,
        customeId: null,
        contact: null,
        memo: null,
        deliveryAddress: null,
        printStatus: null,
        auditsStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询销售送货单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeDeliveryDate && '' != this.daterangeDeliveryDate) {
        this.queryParams.params["beginDeliveryDate"] = this.daterangeDeliveryDate[0];
        this.queryParams.params["endDeliveryDate"] = this.daterangeDeliveryDate[1];
      }
      listDelivery(this.queryParams).then(response => {
        this.deliveryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deliveryNumber: null,
        deliveryDate: null,
        warehouse: null,
        customeId: null,
        customeAddress: null,
        contact: null,
        contactNumber: null,
        customFax: null,
        memo: null,
        deliveryAddress: null,
        contractNumber: null,
        invoiceNumber: null,
        payment: null,
        diver: null,
        courier: null,
        packer: null,
        logistic: null,
        freight: null,
        printStatus: null,
        auditsStatus: null
      };
      this.redDeliverySubList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeDeliveryDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // this.open = true;
      // this.title = "添加销售送货单";
      this.$router.push('/selling/delivery/editDelivery')
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDelivery(id).then(response => {
        this.form = response.data;
        this.redDeliverySubList = response.data.redDeliverySubList;
        this.open = true;
        this.title = "修改销售送货单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.redDeliverySubList = this.redDeliverySubList;
          if (this.form.id != null) {
            updateDelivery(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDelivery(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除销售送货单编号为"' + ids + '"的数据项？').then(function() {
        return delDelivery(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 销售送货单子序号 */
    rowRedDeliverySubIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 销售送货单子添加按钮操作 */
    handleAddRedDeliverySub() {
      let obj = {};
      obj.goodsId = "";
      obj.num = "";
      this.redDeliverySubList.push(obj);
    },
    /** 销售送货单子删除按钮操作 */
    handleDeleteRedDeliverySub() {
      if (this.checkedRedDeliverySub.length == 0) {
        this.$modal.msgError("请先选择要删除的销售送货单子数据");
      } else {
        const redDeliverySubList = this.redDeliverySubList;
        const checkedRedDeliverySub = this.checkedRedDeliverySub;
        this.redDeliverySubList = redDeliverySubList.filter(function(item) {
          return checkedRedDeliverySub.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleRedDeliverySubSelectionChange(selection) {
      this.checkedRedDeliverySub = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('selling/delivery/export', {
        ...this.queryParams
      }, `delivery_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
